package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "分类管理")
@RestController
@RequestMapping("/admin/category")
public class CategoryController {

    private static final Logger log = LoggerFactory.getLogger(CategoryController.class);
    @Autowired
    private CategoryService categoryService;


    /**
     * 分类分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分类分页查询")
    public Result<Map<String,Object>> queryByPage(CategoryPageQueryDTO categoryPageQueryDTO){
        log.info("分类分页查询：{}",categoryPageQueryDTO);
        return categoryService.queryByPage(categoryPageQueryDTO);
    }

    /**
     * 分类修改
     * @param categoryDTO
     * @return
     */
    @ApiOperation("分类修改")
    @PutMapping
    public Result updateByCategory(@RequestBody CategoryDTO categoryDTO){
        log.info("分类修改：{}",categoryDTO);
        return categoryService.updateByCategory(categoryDTO);
    }

    /**
     * 分类启用禁用
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("分类启用禁用")
    public Result startOrStop(@PathVariable("status") Integer status,
                              @RequestParam("id") Integer id){
        log.info("分类状态修改：id:{},status:{}",id,status);
        return categoryService.updateByStatus(status,id);
    }

    /**
     * 新增分类
     * @param categoryDTO
     * @return
     */
    @ApiOperation("新增分类")
    @PostMapping
    public Result insertCategory(@RequestBody CategoryDTO categoryDTO){
        log.info("新增分类:{}",categoryDTO);
        return categoryService.insertCategory(categoryDTO);
    }

    /**
     * 分类删除
     * @return
     */
    @ApiOperation("分类删除")
    @DeleteMapping
    public Result deleteByCategory(@RequestParam("id") Long id){
        log.info("分类删除：id:{}",id);
        return categoryService.deleteById(id);
    }

    /**
     * 分类根据类别查询
     * @param type
     * @return
     */
    @ApiOperation("分类根据类别查询")
    @GetMapping("/list")
    public Result<List<Category>> queryByTypeCategory(@RequestParam("type") Integer type){
        log.info("分类根据类别查询:{}",type);
        return categoryService.queryByType(type);
    }

}
